﻿using System;
using System.Text;
using System.Collections.Generic;
using UnityEngine;
using Framework.Singleton;
using Newtonsoft.Json;

namespace Framework.Util
{
    public class DataUtil : Singleton<DataUtil>
    {
        private DataUtil() { }

        #region DataConversion
        public string ByteArrayToString(byte[] pByteArray)
        {
            return Encoding.UTF8.GetString(pByteArray);
        }

        public byte[] StringToByteArray(string pString)
        {
            return Encoding.UTF8.GetBytes(pString);
        }

        public T JsonToClass<T>(string pJson)
        {
            T tClass = JsonConvert.DeserializeObject<T>(pJson);
            return tClass;
        }

        public string ClassToJson<T>(object pData)
        {
            string json = JsonConvert.SerializeObject(pData);
            return json;
        }
        #endregion

        #region GetData
        public string GetString(object[] pObjectArray, int pIndex)
        {
            if (pIndex >= 0 && pIndex < pObjectArray.Length)
                return pObjectArray[pIndex] as string;
            else
                return string.Empty;
        }

        public T GetClass<T>(object[] pObjectArray, int pIndex)
        {
            string json = GetString(pObjectArray, pIndex);
            return JsonToClass<T>(json);
        }

        public bool GetBool(object[] pObjectArray, int pIndex)
        {
            if (pIndex >= 0 && pIndex < pObjectArray.Length)
                return (bool)pObjectArray[pIndex];
            else
                return false;
        }

        public int GetInt(object[] pObjectArray, int pIndex)
        {
            if (pIndex >= 0 && pIndex < pObjectArray.Length)
                return (int)pObjectArray[pIndex];
            else
                return 0;
        }
        #endregion
    }
}